package com.example.casper.jnuexperiment2025;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class RecyclerViewBookAdapter extends RecyclerView.Adapter<RecyclerViewBookAdapter.BookViewHolder> {
    private List<Book> books;

    public RecyclerViewBookAdapter(List<Book> books) {
        this.books = books;
    }

    @NonNull
    @Override
    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_book, parent, false);
        return new BookViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
        Book book = books.get(position);
        holder.imageView.setImageResource(book.getCoverResourceId());
        holder.textView.setText(book.getTitle() + " - ￥" + book.getPrice());
    }

    @Override
    public int getItemCount() {
        return books.size();
    }

    static class BookViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        BookViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_view_book_cover);
            textView = itemView.findViewById(R.id.text_view_book_name);
            
            // 为itemView启用长按上下文菜单
            itemView.setLongClickable(true);
        }
    }
}